package com.ruoyi.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.domain.Demo;
import com.ruoyi.service.IDemoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 测试模块Controller
 * 
 * @author ruoyi
 * @date 2020-03-18
 */
@Controller
@RequestMapping("/system/demo")
public class DemoController extends BaseController
{
    private String prefix = "system/demo";

    @Autowired
    private IDemoService demoService;

    @RequiresPermissions("system:demo:view")
    @GetMapping()
    public String demo()
    {
        return prefix + "/demo";
    }

    /**
     * 查询测试模块列表
     */
    @RequiresPermissions("system:demo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Demo demo)
    {
        startPage();
        return getDataTable(demoService.list());
    }

    /**
     * 导出测试模块列表
     */
    @RequiresPermissions("system:demo:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Demo demo)
    {
        List<Demo> list = demoService.list(new QueryWrapper<>());
        ExcelUtil<Demo> util = new ExcelUtil<Demo>(Demo.class);
        return util.exportExcel(list, "demo");
    }

    /**
     * 新增测试模块
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存测试模块
     */
    @RequiresPermissions("system:demo:add")
    @Log(title = "测试模块", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Demo demo)
    {
        return toAjax(demoService.save(demo));
    }

    /**
     * 修改测试模块
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Demo demo = demoService.getById(id);
        mmap.put("demo", demo);
        return prefix + "/edit";
    }

    /**
     * 修改保存测试模块
     */
    @RequiresPermissions("system:demo:edit")
    @Log(title = "测试模块", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Demo demo)
    {
        return toAjax(demoService.updateById(demo));
    }

    /**
     * 删除测试模块
     */
    @RequiresPermissions("system:demo:remove")
    @Log(title = "测试模块", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(demoService.removeByIds(Arrays.asList(Convert.toStrArray(ids))));
    }
}
